<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-30
 * Time: 下午3:53
 */

namespace common\services\common;


use common\repositories\common\MyscrmContractProjectRepository;
use common\services\BaseService;
use common\repositories\outstock\DeliveryProductRepository;

class MyscrmContractProjectService extends BaseService
{
    private $repository;

    public function __construct(MyscrmContractProjectRepository $repository, $config = [])
    {
        $this->repository = $repository;

        return parent::__construct($config);
    }

    /**
     * 获取需要需要授权的项目列表
     * @param $orgCode
     * @param $appCode
     * @param $status
     * @param $isOverdue
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getNeedAuthorizeProjectList($orgCode, $appCode, $status, $isOverdue)
    {
        $data = $this->repository->getNeedAuthorizeProjectList($orgCode, $appCode, $status, $isOverdue);

        $data = array_filter($data, function ($item) {
            return implode('', $item) ? true : false;
        });

        return $data;
    }
    public function getAuthorizeProjectList($orgCode, $applicationInfoId, $status, $isOverdue,$keyword)
    {
        /** @var DeliveryProductRepository $deliveryProductRepository */
        $deliveryProductRepository = \Yii::createObject(DeliveryProductRepository::className());
        /** @var MyscrmApplicationService $appService */
        $appService = \Yii::createObject(MyscrmApplicationService::className());
        $result=$deliveryProductRepository->getApplicationIdByAppInfoId($applicationInfoId);
        if(empty($result)){
            return self::setAndReturn(__LINE__, '找不到应用：'.$applicationInfoId );
        }
        $authorize_application_id=$result['authorize_application_id'];
        $appCode = $appService->getAppCodeById($authorize_application_id);
        if (empty($appCode)) {
            return self::setAndReturn(__LINE__, '找不到云客应用：' . $authorize_application_id);
        }
        //如果续签的应用为新版微楼书，需要将旧版微楼的项目也显示出来
        if ($appCode == '5009') {
            $appCode = [5001, 5009];
        }
        $data = $this->repository->getAuthorizeProjectList($orgCode, $appCode, $status, $isOverdue,$keyword);
        $data = array_filter($data, function ($item) {
            return implode('', $item) ? true : false;
        });
        return array_values($data);
    }
    public function getProjectListByContractID($contract_id)
    {
        return $this->repository->getPrjectNameListByContractId($contract_id);
    }

}